package designModel;
// 装饰（Decorator）模式：动态的给对象增加一些职责，即增加其额外的功能。 ☆☆☆☆☆
public class Decorator {
    public static void main(String[] args) {

        Drink da = new CoffeeA();
        System.out.println(da.getDes());
        System.out.println(da.cost());

        // da 订单加牛奶
        da = new MilkDecorator(da);

        System.out.println(da.description());
        System.out.println(da.cost());

        // da 订单加巧克力
        da = new chocolateDecorator(da);
        System.out.println(da.description());
        System.out.println(da.cost());

        // da 订单加巧克力
        da = new chocolateDecorator(da);
        System.out.println(da.description());
        System.out.println(da.cost());

        // = ================================
//        Drink db = new CoffeeB();
//        System.out.println(db.cost());
//
//        Drink dc = new CoffeeC();
//        System.out.println(dc.cost());

    }
}
// 饮料
abstract class Drink {
    String des;
    private int price;

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public abstract double cost();
    public abstract String description();
}

//基本的咖啡类
class Coffee extends Drink{
    @Override
    public double cost() {
        return super.getPrice();
    }

    @Override
    public String description() {
        return null;
    }
}

// CoffeeA
class CoffeeA extends Coffee {
    public CoffeeA() {
        setDes("点个CoffeeA");
        setPrice(10);
    }

}
//CoffeeB
class CoffeeB extends Coffee {
    public CoffeeB() {
        setDes("点个CoffeeB");
        setPrice(20);
    }
}
//CoffeeC
class CoffeeC extends Coffee {
    public CoffeeC() {
        setDes("点个CoffeeC");
        setPrice(30);
    }
}

// 创建装饰器抽象类
abstract class Decorators extends Drink {
    protected Drink drink;

    public Decorators(Drink drink) {
        this.drink = drink;
    }

    @Override
    public double cost() {
        return drink.cost()+super.getPrice();
    }

    @Override
    public String description() {
        // 装饰者的描述 价格 和被装饰者的描述
        return des +""+getPrice() + ",原信息" + drink.getDes();
    }
}

//// 实现具体的装饰器类 （调味品 Milk）
class MilkDecorator extends Decorators {
    public MilkDecorator(Drink drink) {
        super(drink);
        setDes(drink.getDes()+"加牛奶");
        setPrice(3);
    }
}
//// 实现具体的装饰器类 （调味品  chocolate）
class chocolateDecorator extends Decorators {
    public chocolateDecorator(Drink drink) {
        super(drink);
        setDes(drink.getDes()+"加巧克力");
        setPrice(5);
    }
}

//// 实现具体的装饰器类 （调味品  Cookie）
class CookieDecorator extends Decorators {
    public CookieDecorator(Drink drink) {
        super(drink);
        setDes(drink.getDes()+"加甜品");
        setPrice(2);
    }
}

